package cn.edu.sdjzu.xg.bysj.controller.authority;

import cn.edu.sdjzu.xg.bysj.domain.GraduateProject;
import cn.edu.sdjzu.xg.bysj.service.GraduateProjectService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@Slf4j
@WebServlet("/scratch.ctl")
public class ScratchController extends HttpServlet {
    /**
     * POST, http://localhost:8080/scratch.ctl,
     *
     * @param request  请求对象
     * @param response 响应对象
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {

        //根据request对象，获得代表新增GraduateProject的JSON字串
        String req_graduateProjectToAdd_jsonStr = JSONUtil.getJSON(request);
        //将JSON字串解析为GraduateProject对象2
        GraduateProject graduateProjectToAdd = JSON.parseObject(req_graduateProjectToAdd_jsonStr,
                GraduateProject.class);

        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            //在数据库表中增加GraduateProject对象
            GraduateProjectService.getInstance().addScratch(graduateProjectToAdd);
            respMessage_jsonObj.put("message", "增加成功");
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage(), e);
        }
        //响应respMessage_jsonObj到前端
        response.getWriter().println(respMessage_jsonObj);
    }

    /**
     * PUT, http://localhost:8080/graduateProject.ctl, 修改教师
     * <p>
     * 修改一个教师对象：将来自前端请求的JSON对象，更新数据库表中相同id的记录
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String graduateProjectToUpdate_jsonStr = JSONUtil.getJSON(request);
        //将JSON字串解析为要修改的GraduateProject对象（有id的完整对象）
        GraduateProject graduateProjectToUpdate =
                JSON.parseObject(graduateProjectToUpdate_jsonStr, GraduateProject.class);

        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            //到数据库表修改GraduateProject对象对应的记录
            GraduateProjectService.getInstance().updateScratch(graduateProjectToUpdate);
            respMessage_jsonObj.put("message", "修改成功");
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage(), e);
        }
        //响应respMessage_jsonObj到前端
        response.getWriter().println(respMessage_jsonObj);
    }
}
